package com.oocl.view;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.text.ParseException;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import org.json.JSONException;
import org.json.JSONObject;

import com.oocl.constant.LogLevel;
import com.oocl.constant.ProtocolType;
import com.oocl.pojo.Protocol;
import com.oocl.util.JsonUtil;
import com.oocl.util.LogUtil;
import com.oocl.util.ResourceReleaser;

@SuppressWarnings("serial")
public class Register extends JFrame implements ActionListener {
	
	private JLabel titleJLabel;
	private JTextField nameTf;
	private JPasswordField  psdPf1;
	private JPasswordField  psdPf2;
	private JButton loginBtn;
	private JButton registerBtn;
	private JLabel usernameLabel;//login
	private JLabel passwordLabel;//password
	private JLabel confirmLabel;//password
	private JLabel tips;
	
	private String username;
	private String password;
	private String confirm;
	public Register() {
		this.setSize(410, 300);
		this.setResizable(false);//窗体是否可以调整大小
		this.setLocationRelativeTo(null);//位于屏幕中央
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//用户单击窗口的关闭按钮时程序执行的操作
		init();
		addEvent();
		this.setVisible(true);
	}

	private void init(){
		this.setLayout(null);
		nameTf=new JTextField();
		psdPf1=new JPasswordField();
		psdPf2=new JPasswordField();
		loginBtn=new JButton("login");
		registerBtn=new JButton("register");
		titleJLabel=new JLabel("用户注册");
		usernameLabel=new JLabel("username:");
		passwordLabel=new JLabel("password:");
		confirmLabel=new JLabel("confirm:");
		tips=new JLabel();
		
		titleJLabel.setBounds(160,2,230,30);
		titleJLabel.setFont(new Font("宋体", Font.BOLD, 20));
		nameTf.setBounds(120, 30, 230, 40);
		psdPf1.setBounds(120, 80, 230, 40);
		psdPf2.setBounds(120, 130, 230, 40);
		loginBtn.setBounds(120, 200, 90, 40);
		registerBtn.setBounds(260, 200, 90, 40);
		usernameLabel.setBounds(40,30, 70, 30);
		passwordLabel.setBounds(40,80, 70, 30);
		confirmLabel.setBounds(40,130,70,30);
		tips.setBounds(40, 160, 300, 40);
		tips.setForeground(Color.red);
		this.add(titleJLabel);
		this.add(nameTf);
		this.add(psdPf1);
		this.add(psdPf2);
		this.add(loginBtn);
		this.add(registerBtn);
		this.add(usernameLabel);
		this.add(passwordLabel);
		this.add(confirmLabel);
		this.add(tips);
	}
	private void addEvent(){
		registerBtn.addActionListener(this);
		loginBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				Register.this.setEnabled(false);
				Register.this.setVisible(false);
				new Login();
			}
			
		});
	}
	
	@SuppressWarnings("deprecation")
	@Override
	public void actionPerformed(ActionEvent e) {
		if("".equals(nameTf.getText())){
			tips.setText("username can not be empry!");
			return;
		}
		String psd1 = new String(psdPf1.getPassword());
		String psd2 = new String(psdPf2.getPassword());
		if("".equals(psd1.trim())){
			tips.setText("password can not be empty");
			return;
		}
		if(!psd1.equals(psd2)){
			tips.setText("Two password is not same!");
			return;
		}
		tips.setText("");
		username = nameTf.getText();
		Socket socket = null;
		BufferedReader in = null;
		PrintWriter out = null;
		try {
			//10.222.29.201
			socket = new Socket("10.222.29.201", 7777);
			out = new PrintWriter(socket.getOutputStream());
			in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			Protocol protocol = new Protocol();
			protocol.setType(ProtocolType.REGISTER);
			protocol.setFrom(username);
			protocol.setContent(psd2);
			protocol.setTime(new Date());
			out.println(JsonUtil.toJsonObject(protocol).toString());
			out.flush();
			
			String line = in.readLine();
			System.out.println(line);
			JSONObject json1 = new JSONObject(line.trim());
			Protocol pro1 = JsonUtil.toProtocol(json1);
			if(pro1.getType()==ProtocolType.REGISTER_SUCCESS){
				LogUtil.log("register success", LogLevel.INFO);
				tips.setText(pro1.getContent()+"Pls to login view");
			}else {
				LogUtil.log("register fail", LogLevel.INFO);
				tips.setText(pro1.getContent());
				ResourceReleaser.release(out, in, socket);
			}
		} catch (UnknownHostException e1) {
			LogUtil.log("UnknownHostException!"+e1.getMessage(), LogLevel.ERROR);
			JOptionPane.showMessageDialog(null, "找不到服务器连接", "Tip", JOptionPane.ERROR_MESSAGE);
			ResourceReleaser.release(out, in, socket);
		} catch (IOException e1) {
			LogUtil.log("IOException!"+e1.getMessage(), LogLevel.ERROR);
			JOptionPane.showMessageDialog(null, "找不到服务器连接", "Tip", JOptionPane.ERROR_MESSAGE);
			ResourceReleaser.release(out, in, socket);
		} catch (ParseException e1) {
			e1.printStackTrace();
		}
		
	}
	public static void main(String[] args) {
		new Register();
	}
	
}
